//
//  QuitRViewController.m
//  LoverGo
//
//  Created by YeXiao on 14-6-8.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "LGUtil.h"
#import "SVHTTPClient.h"
#import "Toast+UIView.h"
#import "DataCenter.h"
#import "QuitRViewController.h"

@interface QuitRViewController ()

@end

@implementation QuitRViewController

-(id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RegisterNof:) name:registerSPath object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RegisterNof:) name:ErrorNotification object:nil];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
-(void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:registerSPath name:nil object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:ErrorNotification name:nil object:self];
}
-(void)RegisterNof:(NSNotification*)nof
{
    if ([[nof name] compare:registerSPath] == 0) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    if ([[nof name] compare:ErrorNotification] == 0 ) {
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"用户已注册"
                                                                duration:1.0
                                                                position:@"center"];
    }
}
-(void)loadView
{
    
    [self dinglan];
    
    _qView = [[QuitRView alloc] initWithFrame:CGRectMake(0, STATUSBAR +TOPBAR, SCREEN_WIDTH, SCREEN_HEIGHT - (STATUSBAR +TOPBAR))];
    _qView.delegate = self;
    
    [self setView:_qView];
}
-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"注册";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)QuiRView:(int)type
{
    switch (type) {
        case 0:
            [self.navigationController popViewControllerAnimated:YES];
            break;
        case 1:
        {
            //判断
            if ([[_qView.phoneTF text] compare:@""] == 0) {
                [[[[UIApplication sharedApplication] delegate] window] makeToast:@"请输入手机号"
                                                                        duration:1.0
                                                                        position:@"center"];
                return;
            }
            NSDictionary *para = [[NSDictionary alloc] initWithObjectsAndKeys:
                                  [_qView.phoneTF text],@"mobile",
                                  nil];
            [[RequestManager getSingleton] sendRequest:findCheckNumByTelSPath para:para callBack:^(id responseObj) {
                int status = [[responseObj objectForKey:@"status"]intValue];
                NSArray *arr = [responseObj objectForKey:@"data"];
                if (status == 0) {
                    [_qView setSecondView:[[arr objectAtIndex:0] objectForKey:@"checkNum"]];
                    _checkNum =[[arr objectAtIndex:0] objectForKey:@"checkNum"];
                    NSLog(@"%@",_checkNum);
                }else{
                    [[[[UIApplication sharedApplication] delegate] window] makeToast:@"用户已注册"
                                                                            duration:1.0
                                                                            position:@"center"];
                }
            }];
            break;
        }
        case 2:{
            if ([[_qView.phoneTF text] compare:@""] == 0 || ![[_qView.phoneTF text] isEqualToString:_checkNum]) {
                [[[[UIApplication sharedApplication] delegate] window] makeToast:@"请输入正确验证码"
                                                                        duration:1.0
                                                                        position:@"center"];
                return;
            }
            if ([[_qView.pTF text] compare:@""] == 0) {
                [[[[UIApplication sharedApplication] delegate] window] makeToast:@"请输入密码"
                                                                        duration:1.0
                                                                        position:@"center"];
                return;
            }
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setValue:[_qView.phLabel text] forKey:@"mobile"];
            [dic setValue:[_qView.pTF text] forKey:@"passWord"];
            [[RequestManager getSingleton] sendRequestAboutUser:registerSPath para:dic];
            
            break;
        }
        default:
            break;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"注册"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"注册"];
}
@end
